<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
        .uploader-list {
            margin-left: -15px;
        }

        .uploader-list .info {
            position: relative;
            margin-top: -25px;
            background-color: black;
            color: white;
            filter: alpha(Opacity=80);
            -moz-opacity: 0.5;
            opacity: 0.5;
            width: 100px;
            height: 25px;
            text-align: center;
            display: none;
        }

        .uploader-list .handle {
            position: relative;
            background-color: black;
            color: white;
            filter: alpha(Opacity=80);
            -moz-opacity: 0.5;
            opacity: 0.5;
            width: 120px;
            text-align: right;
            height: 18px;
            margin-bottom: -18px;
            display: none;
        }

        .uploader-list .handle i {
            margin-right: 5px;
        }

        .uploader-list .handle i:hover {
            cursor: pointer;
        }

        .uploader-list .file-iteme {
            margin: 12px 0 0 15px;
            padding: 1px;
            float: left;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
	<div class="layui-form-item" style="width: 800px;">
		<input type="hidden" value="${sysMemberInfoEntity.id?default('')}" name="id" id="id"/>
        <label class="layui-form-label required">电话</label>
        <div class="layui-input-block">
            <input type="tel" name="phone" lay-verify="required|phone" lay-reqtext="电话不能为空" placeholder="请输入电话" value="${sysMemberInfoEntity.phone?default('')}" class="layui-input">            
        </div>
    </div>
    <div class="layui-form-item" style="width: 800px;">
        <label class="layui-form-label required">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="realName" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="${sysMemberInfoEntity.realName?default('')}" class="layui-input">            
        </div>
    </div>
   <div class="layui-form-item" style="width: 800px;">
        <label class="layui-form-label required">所属区域</label>
        <div class="layui-input-inline">
	      	<select name="quiz1" id="quizarea1" lay-search="" lay-filter="quizarea1" lay-verify="required" lay-reqtext="请选择省份">
	      	</select>
	    </div>
	    <div class="layui-input-inline">
	      <select name="quiz1" id="quizarea2" lay-search="" lay-filter="quizarea2" lay-verify="required" lay-reqtext="请选择市">
	      </select>
	    </div>
	    <div class="layui-input-inline">
	      <select name="quiz1" id="quizarea3" lay-search="" lay-filter="quizarea3" lay-verify="required" lay-reqtext="请选择县/区">
	      </select>
	    </div>
    </div>
	<div class="layui-form-item">
     	<label class="layui-form-label required">头像照片</label>
	   <div class="layui-upload layui-inline">
		    <div class="layui-upload-list uploader-list">
			    <div id="" class="file-iteme">
			    	<div class="handle" id="handle1"><i class="layui-icon layui-icon-delete"></i></div>
			        <img class="layui-upload-img" src="" onerror="this.src='../images/img.png'" id="demo1" style="width:120px;height:120px">
			    </div>
		    </div>
		    <button type="button" class="layui-btn" id="test1" style="width:120px;">上传头像照片</button>
		    <input type="hidden" name="memberPic" id="demoText1" lay-verify="required" lay-reqtext="请上传头像照片" />
	   </div>
	</div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            <button class="layui-btn layui-btn-normal" onclick="javascript :window.history.go(-1)">返回</button>
        </div>
    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form','upload'], function () {
        var form = layui.form,
            layer = layui.layer,
            upload = layui.upload,
            $ = layui.$;
        
      //初始化联动框
        layer.ready(function(){
        	var companyArea="${sysMemberInfoEntity.memberArea?default('')}";
        	selarea=companyArea.split(",");
        	LayuiCreateSelectArea("quizarea1","../area/getAreaList",selarea[0],"000000");
        	LayuiCreateSelectArea("quizarea2","../area/getAreaList",selarea[1],selarea[0]);
        	LayuiCreateSelectArea("quizarea3","../area/getAreaList",selarea[2],selarea[1]);   	
        	$('#demo1').attr('src', "${sysMemberInfoEntity.memberPic?default('')}");
            $('#demoText1').val("${sysMemberInfoEntity.memberPic?default('')}");	
        });
        
        //初始化表单，要加上，不然刷新部分组件可能会不加载
        form.render();
        var iframeIndex = parent.layer.getFrameIndex(window.name);
        
        //自定义验证规则
        form.verify({     
           phone: [
            	/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/
                , '请输入正确的手机号'
            ]          
        });
        
      //监听提交
        form.on('submit(saveBtn)', function (data) {  

	      	demoText1=$("#quizarea1").val();
	   	 	demoText2=$("#quizarea2").val();
	   	 	demoText3=$("#quizarea3").val();
	   	 	var area="";
		   	if(demoText1!=''&&demoText1!=null&&demoText1!=undefined){
		   		area=demoText1;
		   	}
		   	if(demoText2!=''&&demoText2!=null&&demoText2!=undefined){
		   		if(area==""){
		   			area=demoText2;
		   		}else{
		   			area=area+","+demoText2;
		   		}
		   	}
		   	if(demoText3!=''&&demoText3!=null&&demoText3!=undefined){
		   		if(area==""){
		   			area=demoText3;
		   		}else{
		   			area=area+","+demoText3;
		   		}
		   	}
		   
		   	data.field.memberArea=area;
		   	data.field.id=localStorage.getItem("memberId");
        	$.ajax({
                type : "POST", //提交方式
                url : "update",//路径
                contentType: "application/json;charset=utf-8",
                data : JSON.stringify(data.field),//数据，这里使用的是Json格式进行传输
                dataType: "json",
                success : function(result) {//返回数据根据结果进行相应的处理
                    if (result.code == "200") {
                        var index = layer.msg("修改成功", {
                            time: 1000, //如果不配置，默认是3秒）
                        },function () {
                        	// 关闭弹出层
                        	layer.close(index);
                        	window.open('detail','_self');
                        })
                    }else{
                        layer.msg(result.msg);
                    }
                }
            });
            return false;
        });
      
      //联动
        form.on('select(quizarea1)', function(data){
   		 	LayuiCreateSelectArea("quizarea2","../area/getAreaList","",data.value);
   	 	});
   	 	form.on('select(quizarea2)', function(data){
   		 	LayuiCreateSelectArea("quizarea3","../area/getAreaList","",data.value);
   		});
   	 	
   	 var uploadurl=localStorage.getItem("uploadurl");
     //图片上传上传
       var loading;
       var uploadInst = upload.render({
           elem: '#test1'
           ,accept: 'images' //普通文件
           ,url: uploadurl+'/hnyfkj-file-server/common/upload' //改成您自己的上传接口
           ,before: function(obj){
       		loading= layer.msg('正在上传,请稍后', {icon: 16, shade: 0.3, time:0});
               //预读本地文件示例，不支持ie8
               obj.preview(function(index, file, result){
                   $('#demo1').attr('src', result); //图片链接（base64）
               });
           }
           ,choose: function(obj){
               var param = $('input[name="file"]')[0].value.toString();
               var params = param.split("\.");
               var name = params[0].split("\\");
				var iname = name[name.length-1].replace(/,/, "");
               this.data={name:name[name.length-1].replace(/,/, ""),type:"."+params[params.length-1],system:"im"}
               console.log(this.data);
           }
           ,data: {}
           ,done: function(res){
       		layer.close(loading);
               //如果上传失败
               $("#demoText1").val(uploadurl+"/"+res.result);
               if(res.code != 200){
                   return layer.msg('上传失败');
               }
               //上传成功
           }
           ,error: function(){
               //演示失败状态，并实现重传
               var demoText = $('#demoText1');
               demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
               demoText.find('.demo-reload').on('click', function(){
                   uploadInst.upload();
               });
           }
       });
       
       //删除图片
       $(document).on("mouseenter mouseleave", ".file-iteme", function(event){
           if(event.type === "mouseenter"){
               //鼠标悬浮
               $(this).children(".info").fadeIn("fast");
               $(this).children(".handle").fadeIn("fast");
           }else if(event.type === "mouseleave") {
               //鼠标离开
               $(this).children(".info").hide();
               $(this).children(".handle").hide();
           }
       });

    	// 删除图片
       $(document).on("click", "#handle1", function(event){
       	$('#demo1').attr('src', "");
       	$('#demoText1').val("");
       });   

        function LayuiCreateSelectArea(selectId, url, value,area_id) {//value  设置加载完成时所选定的值
        	if(selectId=="quizarea2"&&area_id==""){
        		$("#quizarea2").empty();//清空该元素
        		$("#quizarea3").empty();//清空该元素
        	}
        	if(selectId=="quizarea3"&&area_id==""){
        		$("#quizarea3").empty();//清空该元素
        	}        	
        	if(selectId=="quizarea2"){
        		$("#quizarea3").empty();//清空该元素
        	}        	
        	if(area_id!=""){
        		//数据请求
        	     $.ajax({
        	          url:url,
        	          type:'POST',
        	          data:{upAreaId:area_id},
        	          datatype:"json",
        	          success:function(suc){
        	        	  data=suc.data;
        	        	//判断id是否有"#"
        	        	  var select_id= selectId;
        	              if (selectId.indexOf('#') != 0) {
        	                  selectId = '#' + selectId;
        	              }
        	              $(selectId).empty();//清空该元素
        	              //创建option
    	              	$(selectId).append('<option value="">请选择</option>');
        	              for (var k in data) {
        	                  $(selectId).append("<option value='" + data[k].areaId + "'>" + data[k].areaName + "</option>");
        	              }
        	              //使用layui下拉框的必要代码
        	              layui.use('form', function () {
        	                  var form = layui.form;
        	                  //设置选中值
        	                  if (value != undefined && value != null && value != '') {
        	                      $(selectId).val(value);
        	                  }
        	                  form.render('select');//重载表单
        	              });
        	          }
        	      });
        	}else{
        		//使用layui下拉框的必要代码
                layui.use('form', function () {
                    var form = layui.form;
                    form.render('select');//重载表单
                });
        	}
        }
    });
</script>
</body>
</html>